package ase.eleitweg.server.db;

import java.util.*;
import ase.eleitweg.common.*;

/**
 * This is a dummy implementation of the UserGroupDAO interface. It doesn't use
 * database or any other form of persistency.
 * 
 * IT'S JUST A DUMMY!!!
 * 
 * @author JZ
 * 
 */
public class UserGroupDummy implements UserGroupDAO {
	
	private List<User> users;
	
	private List<Group> groups;
	
	private static UserGroupDummy instance = null;
	
	public static UserGroupDummy getInstance() {
		if(instance == null) {
			instance = new UserGroupDummy();
		}
		return instance;
	}
	
	private UserGroupDummy() {
		users = new Vector<User>();
		groups = new Vector<Group>();
	}

	public Group getGroupById(int id) {
		for(Group g : groups) {
			if(g.getId() == id) {
				return g;
			}
		}
		return null;
	}

	public List<Group> getGroups() {
		return groups;
	}

	public User getUserById(int id) {
		for(User u : users) {
			if(u.getId() == id) {
				return u;
			}
		}
		return null;
	}

	public List<User> getUsers() {
		return users;
	}

	public List<Group> getUsersGroups(User u) {
		List<Group> result = new Vector<Group>();
		for(Group g : groups) {
			for(User usr : g.getUsers()) {
				if(usr.getId() == u.getId()) {
					result.add(g);
				}
			}
		}
		return result;
	}

	public List<User> getUsersOfGroup(Group g) {
		for(Group grp : groups) {
			if(g.getId() == grp.getId()) {
				return grp.getUsers();
			}
		}
		return null;
	}

	public void createUser(User u) {
		users.add(u);
	}

	public void createGroup(Group g) {
		groups.add(g);
	}

	public void addUser(Group g, User u) {
		for(Group grp : groups) {
			if(grp.getId() == g.getId()) {
				grp.addUser(u);
				return;
			}
		}
	}

	public void updateUser(User u) {
		
	}

	public void updateGroup(Group g) {
		
	}

	public void deleteGroup(Group g) {
		
	}

	public void deleteUser(User u) {
		
	}

}
